﻿var $url = '/content';

var data = {
    apiUrl: utils.getQueryString('apiUrl'),
    siteId: utils.getQueryString('siteId'),
    parentId: '',
    pageLoad: false,
    contents: [],
    pageIndex: 0,
    pageSize: 20,
    sort: 'Id DESC',
    pageCount: 0,
    recordCount: 0,
    state: 0,
    keyword: null
};

var methods = {
    btnDetailsClick: function (id, channel, title) {
        var $this = this;
        utils.openLayer({
            title: '报名详情',
            url: 'enrollLayerDetails.html?siteId=' + $this.siteId + '&id=' + id + "&channel=" + channel + "&title=" + title + "&apiUrl=" + encodeURIComponent(this.apiUrl)
        });
    },
    btnSearchClick: function () {
        var $this = this;
        utils.openLayer({
            title: '学员报名清单',
            url: 'enrollLayerSearchUser.html?siteId=' + $this.siteId + "&key=user" + "&keyword=" + $this.keyword + "&apiUrl=" + encodeURIComponent(this.apiUrl)
        });
    },
    btnCategoryClick: function (category) {
        if (category === 'all') {
            this.parentId = '';
        }

        if (category === 'course') {
            this.parentId = '2';
        }

        if (category === 'activity') {
            this.parentId = '1';
        }

        this.loadPage(0);
    },
    selectState: function () {
        $this = this;
        console.log($this.state);
        this.loadPage(0);
    },
    loadPage: function (pageIndex) {
        var $this = this;

        $api.get($url, {
            params: {
                siteId: this.siteId,
                parentId: this.parentId,
                pageIndex: this.pageIndex,
                pageSize: this.pageSize,
                sort: this.sort,
                state: this.state
            }
        }).then(function (response) {
            //获取报名
            console.log(response.data);
            $this.pageLoad = true;
            $this.contents = response.data.result;
            $this.recordCount = response.data.count;
            $this.pageCount = response.data.count % $this.pageSize === 0 ? parseInt(response.data.count / $this.pageSize) : parseInt(response.data.count / $this.pageSize) + 1;
        });
    },
    loadFirstPage: function () {
        var $this = this;
        $this.clearCheckboxState();
        $this.pageIndex = 0;
        $this.loadPage(0);
    },
    loadPrevPage: function () {
        var $this = this;
        $this.clearCheckboxState();
        if ($this.pageIndex > 0) {
            $this.pageIndex = $this.pageIndex - 1;
            $this.loadPage($this.pageIndex);
        }
    },
    loadNextPage: function () {
        var $this = this;
        $this.clearCheckboxState();
        if ($this.pageIndex < $this.pageCount - 1) {
            $this.pageIndex = $this.pageIndex + 1;
            $this.loadPage($this.pageIndex);
        }
        
    },
    loadLastPage: function () {
        var $this = this;
        $this.clearCheckboxState();
        $this.pageIndex = $this.pageCount - 1;
        $this.loadPage($this.pageIndex);
    },
    clearCheckboxState: function () {
        var checkboxs = document.getElementsByClassName("selectContent");
        for (var i in checkboxs) {
            if (checkboxs[i].checked) {
                checkboxs[i].checked = false;
            }
        }
    },
    btnImportClick: function (id, title) {
        var $this = this;
        utils.openLayer({
            title: '导入接读学员',
            url: 'enrollLayerImportStudents.html?siteId=' + $this.siteId + '&id=' + id + "&title=" + title + "&apiUrl=" + encodeURIComponent(this.apiUrl)
        });
    },
    btnExportClick: function () {
        var checkboxs = document.getElementsByClassName("selectContent");
        var ids = [];
        for (var i in checkboxs) {
            if (checkboxs[i].checked) {
                ids.push(checkboxs[i].value);
            }
        }
        
        $api.get('/enrollment/bycontents', {
            params: {
                ids: ids.join("-")
            }
        }).then(function (response) {
            //console.log(response.data.result);
            //utils.exportJsonToExcel(response.data.result, "报名.xlsx");
            var jsonBook = [];
            
            for (var i in response.data.result) {
                var jsonSheet = {};
                jsonSheet.value = [];
                var tempRow = {};

                if (response.data.result[i].enrollments.length > 0) {

                    var end = response.data.result[i].enrollments[0].title.indexOf("（");
                    if (end > 0) {
                        jsonSheet.title = response.data.result[i].enrollments[0].title.substr(0, end);
                    } else {
                        jsonSheet.title = response.data.result[i].enrollments[0].title;
                    }

                    //清理数据
                    for (var j in response.data.result[i].enrollments) {
                        var item = response.data.result[i].enrollments[j];

                        delete item.id;
                        delete item.contentId;
                        delete item.channelId;
                        delete item.siteId;
                        delete item.tags;
                        delete item.isChecked;
                        delete item.channelName;
                        delete item.userName;
                        delete item.userId;
                        delete item.state;
                        delete item.groupNameCollection;
                        delete item.parentId;

                        tempRow = {};
                        tempRow.姓名 = item.displayName;
                        tempRow.手机 = item.mobile;
                        tempRow.性别 = utils.IdCard(JSON.parse(item.userSettingsXml).citizenCode, 2);
                        tempRow.年龄 = utils.IdCard(JSON.parse(item.userSettingsXml).citizenCode, 3)
                        tempRow.报名时间 = item.enrollmentAddDate;

                        jsonSheet.value.push(tempRow);
                    }
                    
                } else {

                    tempRow = {};
                    tempRow.姓名 = null;
                    tempRow.手机 = null;
                    tempRow.性别 = null;
                    tempRow.年龄 = null;
                    tempRow.报名时间 = null;

                    jsonSheet.value.push(tempRow);
                }

                jsonBook.push(jsonSheet);
            }

            utils.exportJsonToExcel(jsonBook, "报名.xlsx");
        });
    }
};

var $vue = new Vue({
    el: '#main',
    data: data,
    methods: methods,
    computed: {
        
    },
    created: function () {
        this.loadPage(0);
    }
});